import Notify from '@vant/weapp/notify/notify'

const app = getApp()
const util = require('@utils/util.js')
const API = require('@config/api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiper: [{
        image: "https://sixiao.lcol.top/adm/images/1.jpg"
      },
      {
        image: "https://sixiao.lcol.top/adm/images/2.jpg"
      }
    ],
    Content: [],
    code: ""
  },
  btnOrder(e) {
    wx.switchTab({
      url: '/pages/discover/index',
    })
  },
  PushMake(e){
    wx.navigateTo({
      url: '/pages/make/index',
    })
  },
  btnOpenDoor(e) {
    var self = this;
    util.request(
      API.GateQR, {}
    ).then(
      res => {
        if (res.result == 'success') {
          Notify({
            type: 'success',
            message: "请在20秒内开门通过"
          });
        } else {
          console.log(res)
          Notify({
            type: 'warning',
            message: res.message
          });
        }
      },
      err => {
        console.log(err);
      });
  },
  btnPower(e) {
    util.request(
      API.SwitchLight, {}
    ).then(
      res => {
        if (res.result == 'success') {
          Notify({
            type: 'success',
            message: '座位已经供电'
          })
        } else {
          Notify({
            type: 'warning',
            message: res.message
          })
        }
      },
      err => {
        Notify({
          type: 'warning',
          message: err
        })
      });
  },
  btnMessage(e) {

  },
  btnPartern(e) {
    wx.navigateTo({
      url: '/pages/space/ad',
    })
  },
  onChange: function (e) {
    console.log("onChange", e)
    this.setData({
      code: e.detail.value
    })
  },
  /**
   * 美团验券
   */
  meituan: function (e) {
    console.log("美团验券", this.data.code)
    var meituanCode = this.data.code;
    var self = this;
    util.request(
      API.ChangeDeal, {
        shopId: '2',
        meituanCode: meituanCode
      }
    ).then(res => {
      console.log("OK", res.result);
      if (res.result == 'success') {
        wx.navigateTo({
          url: '/pages/person/mycoupon',
        })
      } else {
        Notify({
          message: res.message
        })
      }
    }).catch(error => {
      console.log("ERR", error);
      Notify({
        type: 'warning',
        message: error.msg
      })
    });
  },
  /**
   * 加载活动等广告
   */
  loadAdvert: function () {
    var self = this;
    util.request(API.Brief, {}, "GET").then(result => {
      console.log(result);
      if (result.result == 'success') {
        self.setData({
          brief: result.data
        })
      }
    }).catch(error => {
      console.log(error)
    });
    util.request(API.Activities, {}, "GET").then(result => {
      if (result.result == 'success') {
        self.setData({
          Content: result.data
        })
      }
    }).catch(error => {
      console.log(error)
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.loadAdvert()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})